﻿using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Menu;
using DevExpress.XtraGrid.Views.Grid;
using System;

namespace Smart.Common
{
    public class GridViewColumnButtonMenu : GridViewMenu
    {
        public GridViewColumnButtonMenu(GridView view)
            : base(view)
        {
        }

        protected override void CreateItems()
        {
            base.Items.Clear();
            DXSubMenuItem item = new DXSubMenuItem("Danh S\x00e1ch C\x00e1c Cột");
            item.Image = GridMenuImages.Column.Images[5];
            base.Items.Add(item);
            base.Items.Add(base.CreateMenuItem("Tuỳ Chọn Cột", GridMenuImages.Column.Images[4], "Customization", true));
            base.Items.Add(base.CreateMenuItem("Co Gi\x00e3n Cột", GridMenuImages.Column.Images[6], "BestFit", true));
            base.Items.Add(this.CreateMenuCheckItem("Chọn Hết", false, null, "SelectAll", true, true));
            base.Items.Add(this.CreateMenuItem("Bỏ Chọn", null, "UnSelectAll", true, false));
            foreach (GridColumn column in base.View.Columns)
            {
                if (column.OptionsColumn.ShowInCustomizationForm)
                {
                    item.Items.Add(base.CreateMenuCheckItem(column.GetTextCaption(), column.VisibleIndex >= 0, null, column, true));
                }
            }
        }

        protected override void OnMenuItemClick(object sender, EventArgs e)
        {
            if (!this.RaiseClickEvent(sender, null))
            {
                DXMenuItem item = sender as DXMenuItem;
                if (item.Tag != null)
                {
                    if (item.Tag is GridColumn)
                    {
                        GridColumn tag = item.Tag as GridColumn;
                        tag.VisibleIndex = (tag.VisibleIndex >= 0) ? -1 : base.View.VisibleColumns.Count;
                    }
                    else if (item.Tag.ToString() == "Customization")
                    {
                        base.View.ColumnsCustomization();
                    }
                    else if (item.Tag.ToString() == "BestFit")
                    {
                        base.View.BestFitColumns();
                    }
                    else if (item.Tag.ToString() != "Filter")
                    {
                        if (item.Tag.ToString() == "SelectAll")
                        {
                            base.View.SelectAll();
                        }
                        else if (item.Tag.ToString() == "UnSelectAll")
                        {
                            base.View.ClearSelection();
                        }
                    }
                }
            }
        }
    }
}
